import re
file_name = "./src/zuc/task/taskintf.h"
zuctaskmain_file = "./src/zuc/task/taskintf.cc"
try:
    func_name_list = []
    task_target = []
    copy_target = []
    with open(file_name, "r+", encoding="utf-8") as f:
        lines = f.readlines()
        count = 0
        for line in lines:
            if "extern int zucSetRobot" in line:
                count = count + 1
                pattern = r'zuc.*?\('  
                func_name = re.search(pattern, line).group() # 'zucJointSetMinPositionLimit('
                func_name_list.append(func_name)
                copy_target.append(line)
                continue
            task_target.append(line)
        print(f"All count: {count}")

    with open(file_name, "w+", encoding="utf-8") as f:
        f.writelines(task_target)  
    with open(file_name, "a+", encoding="utf-8") as f:
        f.writelines(copy_target)  
    # with open(zuctaskmain_file, "r+", encoding="utf-8") as f:
    #     lines = f.readlines()
    #     i = 0
    #     while i < len(lines):
    #         line = lines[i]
    #         find = 0
    #         for func_name in func_name_list:
    #             if ("int " + func_name) in line:
    #                 copy_target.append(line)
    #                 i = i + 1
    #                 copy_target.append(lines[i])
    #                 while i < len(lines):
    #                     i = i + 1
    #                     line = lines[i]
    #                     if "int zuc" in line:
    #                         break
    #                     copy_target.append("//" + line)
    #                 copy_target.pop()
    #                 copy_target.pop()
    #                 copy_target.append("return 0;\n }\n\n")
    #                 break
    #         task_target.append(line)
    #         i = i + 1

    # task_main.close()
    # with open(zuctaskmain_file, "w+", encoding="utf-8") as f:
    #     f.writelines(task_target)   
    # with open("./src/zuc/task/taskintf_config.cc", "a", encoding="utf-8") as f:
    #     f.writelines(copy_target)        
except FileNotFoundError:
    print("文件不存在")